﻿Imports System.Windows.Controls.Primitives

<TemplatePart(Name:="PART_I1", Type:=GetType(IntValuedThumb))> _
<TemplatePart(Name:="PART_I0", Type:=GetType(IntValuedThumb))> _
<TemplatePart(Name:="PART_F0", Type:=GetType(IntValuedThumb))> _
<TemplatePart(Name:="PART_F1", Type:=GetType(IntValuedThumb))> _
<TemplatePart(Name:="PART_F2", Type:=GetType(IntValuedThumb))> _
<TemplatePart(Name:="PART_F3", Type:=GetType(IntValuedThumb))> _
Public Class FrequencyNumberView
    Inherits Control

    Shared Sub New()
        DefaultStyleKeyProperty.OverrideMetadata(GetType(FrequencyNumberView), New FrameworkPropertyMetadata(GetType(FrequencyNumberView)))
    End Sub



    Private WithEvents I1 As IntValuedThumb
    Private WithEvents I0 As IntValuedThumb
    Private WithEvents F0 As IntValuedThumb
    Private WithEvents F1 As IntValuedThumb
    Private WithEvents F2 As IntValuedThumb
    Private WithEvents F3 As IntValuedThumb



    Public Property Value As Double
        Get
            Return DirectCast(GetValue(ValueProperty), Double)
        End Get
        Set(ByVal value As Double)
            SetValue(ValueProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ValueProperty As DependencyProperty = DependencyProperty.Register( _
        "Value", GetType(Double), GetType(FrequencyNumberView), _
        New FrameworkPropertyMetadata(0.0, _
            Sub(d, e) DirectCast(d, FrequencyNumberView).OnValueChanged( _
                DirectCast(e.OldValue, Double), DirectCast(e.NewValue, Double))), _
        Function(o) True)

    Protected Overridable Sub OnValueChanged(oldValue As Double, newValue As Double)

        If Value <> Math.Round(newValue * 10000) / 10000 Then

            Value = Math.Round(newValue * 10000) / 10000

        Else

            Dim I = CInt(Math.Round(newValue * 10000))

            Dim v_I1 = (I \ 100000) Mod 10
            Dim v_I0 = (I \ 10000) Mod 10
            Dim v_F0 = (I \ 1000) Mod 10
            Dim v_F1 = (I \ 100) Mod 10
            Dim v_F2 = (I \ 10) Mod 10
            Dim v_F3 = (I \ 1) Mod 10

            Dim b_I1 = False
            Dim b_I0 = True
            Dim b_F0 = False
            Dim b_F1 = False
            Dim b_F2 = False
            Dim b_F3 = False

            If v_I1 <> 0 Then b_I1 = True
            If v_F3 <> 0 Then GoTo l_f3
            If v_F2 <> 0 Then GoTo l_f2
            If v_F1 <> 0 Then GoTo l_f1
            If v_F0 <> 0 Then GoTo l_f0
            GoTo lmao

l_f3:       b_F3 = True
l_f2:       b_F2 = True
l_f1:       b_F1 = True
l_f0:       b_F0 = True
lmao:
            SetIntThumbValues(I1, v_I1, b_I1)
            SetIntThumbValues(I0, v_I0, b_I0)
            SetIntThumbValues(F0, v_F0, b_F0)
            SetIntThumbValues(F1, v_F1, b_F1)
            SetIntThumbValues(F2, v_F2, b_F2)
            SetIntThumbValues(F3, v_F3, b_F3)

        End If

    End Sub

    Private Sub SetIntThumbValues(t As IntValuedThumb, value As Integer, numberShown As Boolean)
        If t IsNot Nothing Then
            t.Value = value
            t.IsNumberShown = numberShown
        End If
    End Sub



    Public Overrides Sub OnApplyTemplate()
        MyBase.OnApplyTemplate()
        I1 = TryCast(GetTemplateChild("PART_I1"), IntValuedThumb)
        I0 = TryCast(GetTemplateChild("PART_I0"), IntValuedThumb)
        F0 = TryCast(GetTemplateChild("PART_F0"), IntValuedThumb)
        F1 = TryCast(GetTemplateChild("PART_F1"), IntValuedThumb)
        F2 = TryCast(GetTemplateChild("PART_F2"), IntValuedThumb)
        F3 = TryCast(GetTemplateChild("PART_F3"), IntValuedThumb)
        OnValueChanged(Value, Value)
    End Sub



    Private origVal As Double

    Protected Sub DragStarted()
        origVal = Value
    End Sub

    Protected Sub DragDelta(change As Double)
        Dim _value = origVal + change
        If _value > 99.9999 Then _value = 99.9999
        If _value < 0 Then _value = 0
        Value = _value
    End Sub

    Private Sub PART_DragStarted(sender As Object, e As DragStartedEventArgs) Handles _
        I1.DragStarted, I0.DragStarted, F0.DragStarted, F1.DragStarted, F2.DragStarted, F3.DragStarted
        DragStarted()
    End Sub

    Private Sub PART_DragDelta(sender As Object, e As DragDeltaEventArgs) Handles _
        I1.DragDelta, I0.DragDelta, F0.DragDelta, F1.DragDelta, F2.DragDelta, F3.DragDelta

        Dim increment As Double = 0
        Select Case True
            Case sender Is I1 : increment = 10
            Case sender Is I0 : increment = 1
            Case sender Is F0 : increment = 0.1
            Case sender Is F1 : increment = 0.01
            Case sender Is F2 : increment = 0.001
            Case sender Is F3 : increment = 0.0001
        End Select

        DragDelta(increment * Math.Floor((-e.VerticalChange + e.HorizontalChange) / 8))

    End Sub

End Class
